//
//  main.m
//  手动内存管理
//
//  Created by FelixYin on 2020/4/12.
//  Copyright © 2020 FelixYin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h"

void test(){
    Student *stu = [[Student alloc] init];
    NSLog(@"%zd",stu.retainCount);
    [stu release];
    
    //使用autorelease 会在适当的时机释放stu对象
    Student *stu2 = [[[Student alloc] init] autorelease];
    stu2.name = @"FelixYin";
    stu2.age = 30;
    NSLog(@"stu2 == %zd  name == %@ age === %zd",stu2.retainCount,stu2.name,stu2.age);
}

void test1(){
    Student *stu2 = [[Student alloc] init];
    stu2.name = @"FelixYin";
    stu2.age = 30;
    
//    Dog *dog = [[[Dog alloc] init] autorelease];
    Dog *dog = [[Dog alloc] init]; //如果这样使用的话dog对象是没有被释放的
    [stu2 setDog:dog];
    [dog release];
    [[stu2 getDog] run];
    [stu2 release];
//    [[stu2 getDog] run]; //这样访问会出现 EXC_BAD_ACCESS 问题
    
}

void test3(){
    Student *stu = [[Student alloc] init];
    
    Dog *dog = [[Dog alloc] init];
    [stu setDog:dog];
    [dog release];
    
    [stu setDog:dog];
    [stu setDog:dog];
    NSLog(@"dog === %@",dog);
    
    [stu release];
    
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        test();
        test3();
    }
    return 0;
}
